﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace learun.util
{
    /// <summary>
    /// 版 本 Learun-ADMS-Core 力软敏捷开发框架
    /// Copyright (c) 2019-present 力软信息技术（苏州）有限公司
    /// 创建人：tobin
    /// 日 期：2019.09.23
    /// 描 述：常用公共类
    /// </summary>
    public static class CommonHelper
    {
        #region 常用方法
        private static long iFeed = 1L;
        private static long iTime = 0L;
        public static long GetUniqueId()
        {
            string s = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            if (iTime.ToString() != s)
            {
                iFeed = 1L;
                iTime = long.Parse(s);
            }
            else
            {
                iFeed += 1L;
            }
            //return ((iTime * 10L) + iFeed);
            return ((iTime * 100L) + iFeed);
        }

        public static DateTime ParseDateTime(string p)
        {
            try
            {
                if (p != string.Empty)
                {
                    return Convert.ToDateTime(p);
                }
                return DateTime.MinValue;
            }
            catch (Exception)
            {
                return DateTime.Now;
            }
        }

        public static decimal ParseDecimal(string p)
        {
            try
            {
                if (p != string.Empty)
                {
                    return Convert.ToDecimal(p);
                }
                return 0M;
            }
            catch (Exception)
            {
                return 0M;
            }
        }

        public static double ParseDouble(string p)
        {
            try
            {
                if (p != string.Empty)
                {
                    return Convert.ToDouble(p);
                }
                return 0.0;
            }
            catch (Exception)
            {
                return 0.0;
            }
        }

        public static int ParseInt(string p)
        {
            try
            {
                if (p != string.Empty)
                {
                    return Convert.ToInt32(p);
                }
                return 0;
            }
            catch (Exception)
            {
                return 0;
            }
        }
        #endregion

        #region Stopwatch计时器
        /// <summary>
        /// 计时器开始
        /// </summary>
        /// <returns></returns>
        public static Stopwatch TimerStart()
        {
            Stopwatch watch = new Stopwatch();
            watch.Reset();
            watch.Start();
            return watch;
        }
        /// <summary>
        /// 计时器结束
        /// </summary>
        /// <param name="watch">Stopwatch</param>
        /// <returns></returns>
        public static string TimerEnd(Stopwatch watch)
        {
            watch.Stop();
            double costtime = watch.ElapsedMilliseconds;
            return costtime.ToString();
        }
        #endregion

        #region 删除数组中的重复项
        /// <summary>
        /// 删除数组中的重复项
        /// </summary>
        /// <param name="values">重复值</param>
        /// <returns></returns>
        public static string[] RemoveDup(string[] values)
        {
            List<string> list = new List<string>();
            for (int i = 0; i < values.Length; i++)//遍历数组成员
            {
                if (!list.Contains(values[i]))
                {
                    list.Add(values[i]);
                };
            }
            return list.ToArray();
        }
        #endregion

        #region 自动生成日期编号
        /// <summary>
        /// 自动生成编号  201008251145409865
        /// </summary>
        /// <returns></returns>
        public static string CreateNo()
        {
            Random random = new Random();
            string strRandom = random.Next(1000, 10000).ToString(); //生成编号 
            string code = DateTime.Now.ToString("yyyyMMddHHmmss") + strRandom;//形如
            return code;
        }
        #endregion

        #region 生成0-9随机数
        /// <summary>
        /// 生成0-9随机数
        /// </summary>
        /// <param name="codeNum">生成长度</param>
        /// <returns></returns>
        public static string RndNum(int codeNum)
        {
            StringBuilder sb = new StringBuilder(codeNum);
            Random rand = new Random();
            for (int i = 1; i < codeNum + 1; i++)
            {
                int t = rand.Next(9);
                sb.AppendFormat("{0}", t);
            }
            return sb.ToString();

        }
        #endregion

        #region 删除最后一个字符之后的字符
        /// <summary>
        /// 删除最后结尾的一个逗号
        /// </summary>
        /// <param name="str">字串</param>
        /// <returns></returns>
        public static string DelLastComma(string str)
        {
            return str.Substring(0, str.LastIndexOf(","));
        }
        /// <summary>
        /// 删除最后结尾的指定字符后的字符
        /// </summary>
        /// <param name="str">字串</param>
        /// <param name="strchar">指定的字符</param>
        /// <returns></returns>
        public static string DelLastChar(string str, string strchar)
        {
            return str.Substring(0, str.LastIndexOf(strchar));
        }
        /// <summary>
        /// 删除最后结尾的长度
        /// </summary>
        /// <param name="str">字串</param>
        /// <param name="Length">删除长度</param>
        /// <returns></returns>
        public static string DelLastLength(string str, int Length)
        {
            if (string.IsNullOrEmpty(str))
                return "";
            str = str.Substring(0, str.Length - Length);
            return str;
        }
        #endregion



        #region 深拷贝
        public static T DeepCopy<T>(T obj)
        {

            string strjson = obj.ToJson();
            return strjson.ToObject<T>();

        }

        public static T DeepCopy<F, T>(F _s)
        {

            string strjson = _s.ToJson();
            return strjson.ToObject<T>();
        }

        public static List<T> DeepCopy<F, T>(List<F> _list)
        {
            List<T> _slist = new List<T>();
            foreach (var _s in _list)
            {

                string strjson = _s.ToJson();
                T _t = strjson.ToObject<T>();

                _slist.Add(_t);

            }

            return _slist;
        }


        #endregion
    }
}
